#include "sensor_app.h"
#include "sht30.h"
#include "led_control.h"
#include "human_detect.h"
#include <stdio.h>
#include "los_task.h"
#include "ohos_init.h"

/* 全局变量 */
float g_humi;
float g_temp;

/* SHT30处理任务 */
static void sht30_process_task(void *arg)
{
    double sht30_data[2] = {0};
    (void)sht30_init();

    while (1) {
        (void)sht30_read_data(sht30_data);
        g_temp = sht30_data[0];
        g_humi = sht30_data[1];
        printf("Temp: %.2fC, Humi: %.2f%%\n", g_temp, g_humi);
        (void)LOS_Msleep(1000);
    }
}

void sensor_app_init(void)
{
    TSK_INIT_PARAM_S tasks[] = {
        {.pfnTaskEntry = (TSK_ENTRY_FUNC)sht30_process_task,
         .uwStackSize = 2048,
         .pcName = "SHT30 Task",
         .usTaskPrio = 23},
         
        {.pfnTaskEntry = (TSK_ENTRY_FUNC)human_check_task,
         .uwStackSize = 2048,
         .pcName = "Human Detect",
         .usTaskPrio = 24},
         
        {.pfnTaskEntry = (TSK_ENTRY_FUNC)led_control_task,
         .uwStackSize = 2048,
         .pcName = "LED Control",
         .usTaskPrio = 25}
    };

    for (int i = 0; i < sizeof(tasks)/sizeof(tasks[0]); i++) {
        unsigned int task_id;
        if (LOS_TaskCreate(&task_id, &tasks[i]) != LOS_OK) {
            printf("Failed to create task %s\n", tasks[i].pcName);
        }
    }
}

APP_FEATURE_INIT(sensor_app_init);
